﻿// Open Source: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Project: YoProgramo.NET (http://code.google.com/p/yoprogramo/)
// Autor: Fabian Figueredo (info@fabianfigueredo.com.ar)
// Blog: blog.yoprogramo.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Validator.Engine;
using NHibernate.Validator.Exceptions;
using YoProgramo.Domain.BusinessLogic.Extensions;
using YoProgramo.Domain.Core.Entities;
using YoProgramo.Domain.Core.Interfaces;
using YoProgramo.Infrastructure.Tools.NHValidator;

namespace YoProgramo.Domain.BusinessLogic.Service
{
    public class UserService : IUserService {

        private IUserRepository _UserRepository;

        public UserService(IUserRepository userrepository)
        {
            _UserRepository = userrepository;
        }

        public InvalidValue[] Create(string username, string password, string email)
        {
            User user = new User();
            user.UserName = username;
            user.Password = password;
            user.Email = email;
            user.RegisteredAt = DateTime.Now;
            user.LastActivityAt = DateTime.Now;
            user.IsActive = true;
            user.IsLockedOut = false;
            user.Age = 0;
            user.Reputation = 0;

            InvalidValue[] validatorErrors = new NHValidatorWrapper<User>().Validate(user);

            if (validatorErrors.Count() < 1){
                user.Password = ExtraTools.EncodeMD5(password);
                _UserRepository.SaveOrUpdate(user);
            }

            return validatorErrors;
        }

        public InvalidValue[] Update(IUser user) {
            InvalidValue[] validatorErrors = new NHValidatorWrapper<User>().Validate((User) user);
            if (validatorErrors.Count() < 1)
                _UserRepository.SaveOrUpdate(user);

            return validatorErrors;
        }


        public bool TryUpdate(IUser user)
        {
            try
            {
                _UserRepository.SaveOrUpdate(user);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public void Delete(string username)
        {
            IUser user =
                _UserRepository
                    .FindByUserName(username);

            if (user != null)
                Delete(user);                
        }

        public void Delete(IUser user)
        {
            _UserRepository.Delete(user);
        }

        public IUser GetByUserName(string username) {
            return _UserRepository.FindByUserName(username);            
        }

        public IUser GetByEmail(string email) {
            return _UserRepository.FindByEmail(email);  
        }

        #region IUserService Members


        public IList<IUser> GetAllNotSpammer() {
            return (from s in GetAll()
                    where !s.IsSpammer()
                    orderby s.GetReputation() descending
                    select s).ToList();
        }

        public IList<IUser> GetAll() {
            return _UserRepository.GetAll();
        }

        #endregion
    }
}